<?php

namespace app\admin\controller\questionnaire\statistics;

use app\BaseController;
use app\model\not_del\UserModel;
use app\model\questionnaire\QuestionnaireLabelModel;
use app\model\questionnaire\QuestionnairePaperModel;
use app\model\questionnaire\QuestionnaireTaskModel;
use app\model\questionnaire\QuestionnaireTaskUserResultModel;
use app\model\questionnaire\QuestionnaireTopicLabelModel;
use app\model\questionnaire\QuestionnaireTopicModel;
use app\model\questionnaire\QuestionnaireTypeModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 问卷任务参与人数统计
     * @return bool|string
     */
    public function peopleNum(): bool|string
    {
        try {
            $QuestionnaireTaskUserResultModel = new QuestionnaireTaskUserResultModel();
            $uid = $QuestionnaireTaskUserResultModel->where('unit_id', '=', $this->userData['unit_id'])->column('uid');
            $uid = array_unique($uid);
            $UserModel = new UserModel();
            $data['count'] = count($uid);
            $data['boy'] = $UserModel->where([
                ['id', 'in', $uid],
                ['sex', '=', 1]
            ])->count('id');
            $data['girl'] = $data['count'] - $data['boy'];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 参与人数时段统计
     * @return bool|string
     */
    public function time(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'type|类型' => 'require|integer|in:1,2,3',
            ]);
            $time = match ($param['type']) {
                '1' => custom_time([], 'y'),
                '2' => custom_time([], 'm'),
                '3' => custom_time(),
                default => throw new \Exception('参数错误！'),
            };
            $data = [];
            $QuestionnaireTaskUserResultModel = new QuestionnaireTaskUserResultModel();
            $UserModel = new UserModel();
            foreach ($time as $item){
                $data['date'][] = $item['value'];
                $start_time = strtotime($item['start_time']);
                $end_time = strtotime($item['end_time']);
                $uid = $QuestionnaireTaskUserResultModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['time','between',[$start_time,$end_time]]
                ])->column('uid');
                $uid = array_unique($uid);
                if (empty($uid)){
                    $data['boy'][] = 0;
                    $data['girl'][] = 0;
                    continue;
                }
                $total = count($uid);
                $boy = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
                $girl = $total - $boy;
                $data['boy'][] = $boy;
                $data['girl'][] = $girl;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 问卷库统计
     * @return bool|string
     */
    public function paperType(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $QuestionnaireTypeModel = new QuestionnaireTypeModel();
            $QuestionnairePaperModel = new QuestionnairePaperModel();
            $data = $QuestionnaireTypeModel->field('id,name')
                ->where('unit_id', '=', $this->userData['unit_id'])->withCount(['paper'])->paginate($param['pageSize'])->toArray();
            $typeCount = $QuestionnaireTypeModel->where('unit_id', '=', $this->userData['unit_id'])->count('id');
            $count = $QuestionnairePaperModel->where('unit_id', '=', $this->userData['unit_id'])->count('id');
            return success([
                'typeCount' => $typeCount,
                'count' => $count,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 题目类型统计
     * @return bool|string
     */
    public function topicType(): bool|string
    {
        try {
            $QuestionnaireTopicModel = new QuestionnaireTopicModel();
            $data[] = $QuestionnaireTopicModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['type','=',1],
                ['status','=',1]
            ])->count();
            $data[] = $QuestionnaireTopicModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['type','=',2],
                ['status','=',1]
            ])->count();
            $data[] = $QuestionnaireTopicModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['type','=',3],
                ['status','=',1]
            ])->count();
            $data[] = $QuestionnaireTopicModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['type','=',4],
                ['status','=',1]
            ])->count();
            $data[] = $QuestionnaireTopicModel->where([
                ['unit_id', '=', $this->userData['unit_id']],
                ['type','=',5],
                ['status','=',1]
            ])->count();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 题目标签统计
     * @return bool|string
     */
    public function label(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $QuestionnaireLabelModel = new QuestionnaireLabelModel();
            $QuestionnaireTopicLabelModel = new QuestionnaireTopicLabelModel();
            $query = $QuestionnaireLabelModel->where('unit_id','=',$this->userData['unit_id']);
            $data = $query->field('id,name')
                ->withCount(['topicLabel'])->paginate($param['pageSize'])->toArray();
            $ids = $query->column('id');
            $data['count'] = $QuestionnaireTopicLabelModel->where('label_id', 'in', $ids)->count();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 答题结果统计
     * @return bool|string
     */
    public function result(): bool|string
    {
        try {
            $QuestionnaireTaskModel = new QuestionnaireTaskModel();
            $QuestionnaireTaskUserResultModel = new QuestionnaireTaskUserResultModel();
            $user_num = $QuestionnaireTaskModel->where('unit_id','=',$this->userData['unit_id'])->sum('user_num');
            $paper_num = $QuestionnaireTaskModel->where('unit_id','=',$this->userData['unit_id'])->sum('paper_num');
            $data['total'] = $user_num * $paper_num;
            $data['result'] = $QuestionnaireTaskUserResultModel->where('unit_id','=',$this->userData['unit_id'])->count();
            $data['pass'] = $QuestionnaireTaskUserResultModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_pass','=',1]
            ])->count();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}